package com.springark.ms.springarkgateway.limiter;

import com.springark.ms.commons.consts.SpringarkRequestHeaders;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;
import java.util.Optional;

/**
 * 根据AppId实现限流
 * @author hankchan
 * @since 2019/5/11
 */
public class AppIdKeyResolver implements KeyResolver {

    public static final String BEAN_NAME = "appIdKeyResolver";

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(Optional.ofNullable(
                exchange.getRequest().getHeaders().getFirst(SpringarkRequestHeaders.APP_ID)).orElse(""));
    }
}
